const dataBaseMethods = require('../db')
const tool = require('../tools/tool')

module.exports = {
    parseValue(type, value) {
        if (type == 'string') {
            return value.toString()
        }
        if (type == 'number') {
            return parseFloat(value)
        }
        if (type == 'date') {
            return new Date(value)
        }
        if (type == 'object') {
            if (typeof (value) == 'object') return value
            return JSON.parse(value)
        }
        if (type == 'boolean') {
            return value.toString() == 'true' ? true : false
        }
        return value
    },
    async getItem(key) {
        let re = await dataBaseMethods.select("select * from settings where `key` = '" + key.toString() + "'")
        if (re.length) {
            return this.parseValue(tool.JSON.parse(re[0].value).type, tool.JSON.parse(re[0].value).value)
        } else {
            return null
        }
    },
    async getItemInfo(key) {
        let re = await dataBaseMethods.select("select * from settings where `key` = '" + key.toString() + "'")
        if (re.length) {
            let t = re[0]
            t.valueParsed = this.parseValue(tool.JSON.parse(re[0].value).type, tool.JSON.parse(re[0].value).value)
            return t
        } else {
            return undefined
        }
    },
    async setItem(key, value, explain = undefined) {
        if (!key) {
            throw new Error(`未指定key`)
        }
        let t
        if ((await this.getItem(key)) != null) {
            t = await this.getItem(key)
            await this.removeItem(key)
        }
        await dataBaseMethods.insertInto('settings', {
            key: key,
            value: JSON.stringify({
                type: typeof (value),
                value: value
            }),
            explain: explain == undefined ? t.explain : explain
        })
    },
    async removeItem(key) {
        await dataBaseMethods.exe(`delete from settings where \`key\` = '${key}'`)
    },
    list: [
        // {
        //     key: 'allowRegister_businessUser',
        //     default: true,
        //     explain: '允许注册 - 企业用户'
        // },
        // {
        //     key: 'allowRegister_platformAdmin',
        //     default: true,
        //     explain: '允许注册 - 平台管理员'
        // },
        // {
        //     key: 'allowRegister_consumer',
        //     default: true,
        //     explain: '允许注册 - 消费者'
        // },
        {
            key: 'banner',
            default: [
                "b1.png",
                "b2.png",
                "b3.png"
            ],
            explain: '轮播图'
        },
        {
            key: 'role',
            default: [
                "学生",
                "老师"
            ],
            explain: '用户类型'
        },
        {
            key: 'accountVer',
            default: true,
            explain: '新用户审核过后才能登录使用'
        },
        {
            key: 'maxDelayTime',
            default: 30,
            explain: '巡查延期最长天数'
        },
        {
            key: 'newPatrolDefaultWorkDays',
            default: 3,
            explain: '巡查默认截止时间几天之后'
        },
        {
            key: 'progress_feedBack',
            default: true,
            explain: '巡查评价流程'
        },
        {
            key: 'allowReg',
            default: false,
            explain: '显示注册按钮'
        },
        {
            key: 'progress_review',
            default: true,
            explain: '巡查审核流程'
        },
        {
            key: 'postWeChatMessage',
            default: true,
            explain: '发送微信服务通知'
        },
        {
            key: 'sendMessage',
            default: true,
            explain: '发送手机短信'
        },
        {
            key: 'dailyPatrolLimitPerUser',
            default: 10,
            explain: '每人每天可新建巡查数量'
        },
        {
            key: 'logFrontStatus',
            default: true,
            explain: '输出前端状态'
        },
        {
            key: 'handlerDailyReport',
            default: true,
            explain: '负责人日报'
        },
        {
            key: 'newPatrolSendNotificationWaitTime',
            default: 0,
            explain: '新建巡查之后等多长时间再发送通知'
        },
    ],
    async init() {
        let currentList = await dataBaseMethods.select(`select * from settings`)
        for (let i = 0; i < currentList.length; i++) {
            currentList[i].valueParsed = this.parseValue(tool.JSON.parse(currentList[i].value).type, tool.JSON.parse(currentList[i].value).value)
        }
        const exist = (key) => {
            for (let i = 0; i < currentList.length; i++) {
                if (currentList[i].key == key) {
                    return true
                }
            }
            return false
        }
        for (let i = 0; i < this.list.length; i++) {
            if (exist(this.list[i].key) == false) {
                await this.setItem(this.list[i].key, this.list[i].default, this.list[i].explain)
            }
        }
    }
}